class ResourceKeyword < ActiveRecord::Base
  establish_connection :discourse_test
  set_table_name 'resources_keywords'
  
  #ruby script\runner ResourceKeyword.migrate
  def self.migrate
    rks = ResourceKeyword.find_by_sql(["SELECT k.keyword, r.title, r.url, r.description FROM keywords k, resources_keywords rk, resources r WHERE k.id = rk.keyword_id AND r.id = rk.resource_id"])
    
    rks.each do |rk|
      r = Resource.find_by_title(rk.title)
      r = Resource.create(:title => rk.title, :content => "#{rk.url} #{rk.description}")if r.nil?
      k = Keyword.find_by_title(rk.keyword)
      k = Keyword.create(:title => rk.keyword, :content => rk.keyword ) if k.nil?
      Relation.create("object_id"=>r.id, "subject_id"=>k.id)
    end
  end
end
